package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.query.BpmnInfoQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 服务项目服务impl
 *
 * @author RNIZ
 * @date 2021/11/13
 */
@Service
@Transactional
public class BpmnInfoServiceImpl implements IBpmnInfoService {


    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private RepositoryService repositoryService;

    @Override
    public TablePageInfo<BpmnInfo> query(BpmnInfoQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BpmnInfo>(bpmnInfoMapper.selectForList(qo));
    }

    @Override
    public BpmnInfo get(Long id) {
        return bpmnInfoMapper.selectByPrimaryKey(id);
    }


    @Override
    public void save(BpmnInfo bpmnInfo) {
        bpmnInfoMapper.insert(bpmnInfo);
    }

    @Override
    public void deleteBatch(Long ids) {
        //获取bpmninfo对象
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(ids);
        //删除bpmninfo对象
        bpmnInfoMapper.deleteByPrimaryKey(ids);
        //获取流程定义对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(bpmnInfo.getDeploymentId()).singleResult();
        //删除流程定义对象
        repositoryService.deleteDeployment(bpmnInfo.getDeploymentId(),true);
        //获取流程定义的名字
        String resourceName = processDefinition.getResourceName();
        //通过名字获取文件
        File file = new File(SystemConfig.getProfile(), resourceName);
        //判断文件是否存在
        if (file.exists()) {
            //删除文件
            file.delete();
        }
    }

    @Override
    public void insert(String path, String bpmnType, String info) throws FileNotFoundException {

        //根据path找到流程文件
        File bpmnFile = new File(SystemConfig.getProfile(), path);
        //2将流程文件部署到activiti中
        Deployment deployment = repositoryService.createDeployment().
                addInputStream(path, new FileInputStream(bpmnFile)).deploy();
        //根据生成的部署对象,流程对象,封装bpmnInfo对象
        BpmnInfo bpmnInfo = new BpmnInfo();
        bpmnInfo.setDeploymentId(deployment.getId());
        bpmnInfo.setInfo(info);
        bpmnInfo.setDeployTime(new Date());
        bpmnInfo.setBpmnType(bpmnType);
        //获取流程定义对象
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).singleResult();
        bpmnInfo.setActProcessId(processDefinition.getId());
        bpmnInfo.setActProcessKey(processDefinition.getKey());
        bpmnInfo.setBpmnName(processDefinition.getName());
        bpmnInfoMapper.insert(bpmnInfo);

    }

    @Override
    @Transactional
    public InputStream getResourceAsStream(String deploymentId, String type) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
        if ("xml".equalsIgnoreCase(type)) {
            //xml
            String resourceName = processDefinition.getResourceName();
            return repositoryService.getResourceAsStream(deploymentId,resourceName);
        }else {
            BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
            DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            return generator.generateDiagram(bpmnModel, Collections.EMPTY_LIST,Collections.EMPTY_LIST,"宋体","宋体","宋体");

        }
    }

    @Override
    public BpmnInfo getByType(String car_package) {
        return bpmnInfoMapper.getByTuye(car_package);

    }

    @Override
    public void update(BpmnInfo bpmnInfo) {
        bpmnInfoMapper.updateByPrimaryKey(bpmnInfo);
    }

    @Override
    public List<BpmnInfo> list() {
        return bpmnInfoMapper.selectAll();
    }

}
